<%@page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@page import="java.sql.Driver" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.DriverManager" %>
<%@page import="java.util.ArrayList" %>
<%@page import="java.util.Map" %>
<%@page import="java.util.HashMap" %>
<%@page import="java.util.Iterator" %>
<%@page import="java.io.*" %>
<% int validaSession = 4; %>
<%@ include file="Connections/conDB.jsp" %>
<%@ include file="Connections/session.jsp" %>
<%
	final String CONTRATO = "arrL_contrato"; 
	final String VOID      = "";
	final String ACCION    = "accion";
	final String CODIGO    = "Codigo";
	final String NOMBRE    = "Nombre";
	final String ADD       = "agregar";
	final String DELETE    = "eliminar";
	final String FLUSH     = "vaciar";
	final boolean DEBUG    = false;
	
	String str_nombre_proveedor = request.getParameter("nombre_contrato");
	String str_codigo_proveedor = request.getParameter("codigo_contrato");
	String str_accion = request.getParameter(ACCION);
	String str_posicion = request.getParameter("posicion");
	int int_posicion = str_posicion!=null?Integer.parseInt(str_posicion):0;
	
	String str_parametro_busqueda = request.getParameter("txt_busqueda");
	str_parametro_busqueda = str_parametro_busqueda==null?VOID:str_parametro_busqueda;
	
	Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
	Connection conn     = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
	ArrayList arrL_contrato = (ArrayList)session.getAttribute(CONTRATO);
	if(arrL_contrato!=null){
		if(str_codigo_proveedor!=null && str_nombre_proveedor != null){
			if(str_accion.equals(ADD)){
				Map map_contrato = new HashMap();
				map_contrato.put(CODIGO, str_codigo_proveedor);
				map_contrato.put(NOMBRE, str_nombre_proveedor);
				arrL_contrato.add(map_contrato);
				session.setAttribute(CONTRATO, arrL_contrato);
			}
			if(str_accion.equals(DELETE)){
				// Comparator c = arrL_provedores.
				arrL_contrato.remove(int_posicion);
			}
			if(str_accion.equals(FLUSH)){
				arrL_contrato.clear();
				session.setAttribute(CONTRATO, arrL_contrato);
			}
		}
	}else{
		arrL_contrato = new ArrayList();
		session.setAttribute(CONTRATO, arrL_contrato);
	}
	
	try{
		String str_query_proveedor = 
			" SELECT  ID_CONTRATO                                              " + 
			"        ,DESCRIPCION                                              " +
			"        ,FECHA                                                    " +
			"        ,CANTIDAD                                                 " +
			"        ,ESTATUS                                                  " +
			"        ,OBSERVACIONES                                            " +
			"        ,CANT_CONSUMIDA                                           " +       
			"        ,ACTIVAHUMEDAD                                            " +
			"        ,HUMEDAD_BASE                                             " +
			"        ,LOTE                                                     " +
			"        ,TIPO_CAMBIO                                              " +
			"        ,TIPO_BUSHEL                                              " +
			" FROM cimav2.dbo.contrato                                         " +
			" WHERE 1=1                                                        " +
			"  		AND (ID_CONTRATO LIKE '%"+str_parametro_busqueda+"%'       " + 
			"        OR DESCRIPCION    LIKE '%"+str_parametro_busqueda+"%'    )";
		
		if(arrL_contrato.size()>0){
			str_query_proveedor += " AND ID_CONTRATO not in('' ";
		}
		Map map_proveedor_query = new HashMap();
		Iterator it_query = arrL_contrato.iterator();
		while(it_query.hasNext()){
			map_proveedor_query =  (HashMap)it_query.next();
			str_query_proveedor += ",'" + map_proveedor_query.get(CODIGO)+"'";
		}
		if(arrL_contrato.size()>0){
			str_query_proveedor += ")";
		}
		if(DEBUG){out.println(str_query_proveedor);}
		ResultSet rs = conn.createStatement().executeQuery(str_query_proveedor);
%>
<html>
	<head>
		<title>Cima v2 ~ Busqueda Contratos</title>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/AnchorPosition.js"></SCRIPT>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/PopupWindow.js"></SCRIPT>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/date.js"></SCRIPT>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/CalendarPopup.js"></SCRIPT>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/tooltip.js"></SCRIPT>
		<SCRIPT language="javascript" src="jsV21/util.js"></script>
		<SCRIPT LANGUAGE="JavaScript" SRC="js/tooltip.js"></SCRIPT>
		<SCRIPT language="javascript">
			var cal1 = new CalendarPopup();
			var cal2 = new CalendarPopup();
			function LimpiarForma( forma ){
				forma.txt_fechaD.value = '';
				forma.txt_fechaH.value = '';
				forma.txt_fechaPD.value = '';
				forma.txt_fechaPH.value = '';
				forma.txt_proveedor.value = '';
				forma.txt_contrato.value = '';
				forma.txt_noDocumento.value = '';
				forma.txt_inicial.value = '';
				forma.txt_numero.value = '';
				forma.sel_estatusPago.selectedIndex = 0;
			}
			
			function ValidarForma( forma ){
				var arr_desde = forma.txt_fechaD.value.split( '/' );
				var dDesde = new Date( arr_desde[2], arr_desde[1]-1, arr_desde[0] );
				
				var arr_hasta = forma.txt_fechaH.value.split( '/' );
				var dHasta = new Date( arr_hasta[2], arr_hasta[1]-1, arr_hasta[0] );
						
				if( dDesde > dHasta ){
					alert( "Fecha Desde debe ser menor o igual a Fecha Hasta!" );
				}else{
					forma.submit();
				}
			}
		</SCRIPT>
		<link href="miestilo.css" rel="stylesheet" type="text/css">
		<style type="text/css">
		<!--
		body {
			margin-left: 0px;
			margin-top: 0px;
			margin-right: 0px;
			margin-bottom: 0px;
		}
		.filtro{
			background-color:#cde5b1;
		}
		-->
		</style>
	</head>
<body background="images/fondo.jpg" onload="self.focus();">
<form action="reporte_entrada_contrato_contrato.jsp" method="post">
	<table height="100%" width="100%">
		<tr>
			<td height="50%" valign="top">
				<table width="100%" border="0" cellpadding="0" cellspacing="1">
					<tr>
						<td colspan="3" align="center" class="titulos"  height="24"> Codigo/Nombre &nbsp;&nbsp; <input type="text"  name="txt_busqueda" id="txt_busqueda" size="30" maxlength="30" value="<%= str_parametro_busqueda%>"class="textcolumna"/> &nbsp;&nbsp; <input type="submit" value="Buscar" class="boton"/> </td>
					</tr>
					<tr>
						<td class="titcolumna" width="5%" height="20" align="center" >&nbsp;</td>
						<td class="titcolumna" width="15%" align="center">Codigo</td>
						<td class="titcolumna" width="80%" align="center">Descripci&oacute;n</td>
					</tr>
					<% 
					if(request.getParameter("txt_busqueda")!=null){
						String str_codigo;
						String str_descripcion;
						int int_i = 0;
						while(rs.next()){
							str_codigo      = rs.getString("ID_CONTRATO");
							str_descripcion = rs.getString("DESCRIPCION");
					%>
					<tr class="<%=int_i%2==0?"textcolumna":"textcolumnaNon"%>">
						<td align="center" height="18"><a href="reporte_entrada_contrato_contrato.jsp?accion=<%=ADD %>&codigo_contrato=<%=str_codigo %>&nombre_contrato=<%=str_descripcion %>&txt_busqueda=<%=str_parametro_busqueda%>&posicion=<%=int_i%>"><img src="images/img_ok.jpg" border="0" alt="Agregar Contrato..." /></a></td>
						<td align="center"><%=str_codigo %></td>
						<td align="left">&nbsp;<%=str_descripcion %></td>
					</tr>
					<%		int_i++;
						}
					}
					%>
				</table>
			</td>
		</tr>
		<tr>
			<td height="50%" valign="top">
				<table width="100%" border="0" cellpadding="0" cellspacing="1">
					<tr>
						<td colspan="3" align="center" class="titulos" height="24">Contratos Seleccionados &nbsp;</td>
					</tr>
					<tr>
						<td class="titcolumna" width="5%" height="20" align="center">
							<a href="reporte_entrada_contrato_contrato.jsp?accion=<%=FLUSH %>&txt_busqueda=<%=str_parametro_busqueda%>&nombre_contrato=&codigo_contrato=">
								<img src="images/trash.gif" border="0" alt="Eliminar Contratos..." />
							</a>
						</td>
						<td class="titcolumna" width="15%" align="center">Codigo</td>
						<td class="titcolumna" width="80%" align="center">Descripci&oacute;n</td>
					</tr>
					<%
					Iterator it = arrL_contrato.iterator();
					Map map_iterate = null; 
					int int_i = 0;
					while(it.hasNext()){
						map_iterate = (HashMap) it.next();
					%>
					<tr class="<%=int_i%2==0?"textcolumna":"textcolumnaNon"%>">
						<td align="center"  height="18" >
							<a href="reporte_entrada_contrato_contrato.jsp?accion=<%=DELETE %>&codigo_contrato=<%=map_iterate.get(CODIGO) %>&nombre_contrato=<%=map_iterate.get(NOMBRE) %>&txt_busqueda=<%=str_parametro_busqueda%>&posicion=<%=int_i%>">
								<img src="images/delete2.jpg" border="0" alt="Eliminar Contrato <%=map_iterate.get(CODIGO)%> - <%=map_iterate.get(NOMBRE)%> " />
							</a>
						</td>
						<td align="center"><%=map_iterate.get(CODIGO)  %>     </td>
						<td align="left">&nbsp;<%=map_iterate.get(NOMBRE)  %> </td>
					</tr>
					<%
						int_i++;
					}
					%>
				</table>
			</td>
		</tr>
	</table>

<%
	}catch(Exception e){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		StringBuffer error = sw.getBuffer();
		out.println(error);
	}finally{
		conn.close();
	}
 %>
 </form>
</body>
</html>